package com.foodcommunity.controller;

import com.foodcommunity.common.Result;
import com.foodcommunity.dto.DataAnalysisDTO;
import com.foodcommunity.entity.User;
import com.foodcommunity.service.DataAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 数据分析控制器
 */
@RestController
@RequestMapping("/api/analysis")
public class DataAnalysisController {

    @Autowired
    private DataAnalysisService dataAnalysisService;

    /**
     * 获取总体数据分析
     */
    @GetMapping("/overall")
    public Result<DataAnalysisDTO> getOverallAnalysis(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return Result.error("请先登录");
        }
        
        DataAnalysisDTO analysisDTO = dataAnalysisService.getOverallAnalysis();
        return Result.success(analysisDTO);
    }

    /**
     * 获取月度趋势数据
     */
    @GetMapping("/monthly-trends")
    public Result<List<Map<String, Object>>> getMonthlyTrends(
            @RequestParam(defaultValue = "12") int months,
            HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return Result.error("请先登录");
        }
        
        List<Map<String, Object>> trends = dataAnalysisService.getMonthlyTrends(months);
        return Result.success(trends);
    }

    /**
     * 获取用户偏好分析
     */
    @GetMapping("/user-preferences")
    public Result<Map<String, Object>> getUserPreferences(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return Result.error("请先登录");
        }
        
        Map<String, Object> preferences = dataAnalysisService.getUserPreferences(user.getId());
        return Result.success(preferences);
    }

    /**
     * 获取个性化推荐的帖子
     */
    @GetMapping("/recommended-posts")
    public Result<List<Map<String, Object>>> getRecommendedPosts(
            @RequestParam(defaultValue = "10") int limit,
            HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return Result.error("请先登录");
        }
        
        List<Map<String, Object>> recommendedPosts = dataAnalysisService.getRecommendedPosts(user.getId(), limit);
        return Result.success(recommendedPosts);
    }

    /**
     * 获取个性化推荐的餐厅
     */
    @GetMapping("/recommended-restaurants")
    public Result<List<Map<String, Object>>> getRecommendedRestaurants(
            @RequestParam(defaultValue = "10") int limit,
            HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return Result.error("请先登录");
        }
        
        List<Map<String, Object>> recommendedRestaurants = dataAnalysisService.getRecommendedRestaurants(user.getId(), limit);
        return Result.success(recommendedRestaurants);
    }

    /**
     * 获取热门美食排行榜
     */
    @GetMapping("/hot-food-ranking")
    public Result<List<Map<String, Object>>> getHotFoodRanking(
            @RequestParam(defaultValue = "10") int limit) {
        List<Map<String, Object>> ranking = dataAnalysisService.getHotFoodRanking(limit);
        return Result.success(ranking);
    }

    /**
     * 获取热门餐厅排行榜
     */
    @GetMapping("/hot-restaurant-ranking")
    public Result<List<Map<String, Object>>> getHotRestaurantRanking(
            @RequestParam(defaultValue = "10") int limit) {
        List<Map<String, Object>> ranking = dataAnalysisService.getHotRestaurantRanking(limit);
        return Result.success(ranking);
    }
} 